import axios from 'axios'

export default {
    namespaced: true,
    state: {
        id: 0,
        keyword: "",
        list: [],
        typeItems: [],
        page: 1,
        limit: 10,
        total: 0,
    },
    actions: {
        // 文章信息
        getList: ({commit, state}) => {
            axios.get("/vue/articles/list", {
                params: {
                    type_id: state.id,
                    keyword: state.keyword,
                    limit: state.limit,
                    offset: (state.page - 1) * state.limit
                }
            }).then(response => {
                if (response.data.code === 0) {
                    commit("setList", response.data.data.items);
                    commit("setTotal", response.data.data.total);
                }
            })
        },

        // 文章分类
        getArticleTypes: ({commit}) => {
            axios.get("/vue/article-types/index").then((response) => {
                if (response.data.code === 0) {
                    commit("setArticleTypes", response.data.data);
                }
            })
        }
    },

    mutations: {
        // 设置文章ID
        setTypeId(state, id) {
            state.id = id;
        },

        // 分页
        setPage(state, page) {
            state.page = page;
        },

        // 搜索关键字
        searchKeyWord(state, keyword) {
            state.keyword = keyword;
        },

        // 设置文章信息
        setList(state, list) {
            state.list = list || []
        },

        // 设置文章分类信息
        setArticleTypes(state, items) {
            state.typeItems = items || [];
        },

        setTotal(state, total) {
            state.total = total
        }
    }
}

